package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.LiveRoomInfo;
import com.ymatou.iapi.livebuyer.parameter.QueryByShopIndexBean;
import com.ymatou.iapi.livebuyer.service.QueryByShopIndexCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*直播小铺首页列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryByShopIndex{
	private static QueryByShopIndexBean querybyshopindexBean;
	private static QueryByShopIndexCall querybyshopindexCall;
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播小铺首页列表");
	}

	@Before
	public void caseUp() {
		querybyshopindexBean=new QueryByShopIndexBean();
		querybyshopindexCall=new QueryByShopIndexCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querybyshopindexCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(LiveRoomInfo liveRoomInfo) {
		LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveRoomInfo.getLiveId());
		Logger.verifyEquals(liveroom.getStartTime().substring(0, 19), liveRoomInfo.getStartTime(), "验证StartTime");
		Logger.verifyEquals(liveroom.getCreateTime().substring(0, 19), liveRoomInfo.getCreateTime(), "验证CreateTime");
		Logger.verifyEquals(liveroom.getGroupId(), liveRoomInfo.getGroupId(), "验证GroupId");
		// Logger.verifyEquals(liveroom.getHistoryPlayUrl(),
		// liveRoomInfo.getHistoryPlayUrl(), "验证HistoryPlayUrl");
		Logger.verifyEquals(liveroom.getLiveCover(), liveRoomInfo.getLiveCover(), "验证LiveCover");
		Logger.verifyEquals(liveroom.getId(), liveRoomInfo.getLiveId(), "验证LiveId");
		// 修改status
		if (liveroom.getLiveStatus() == 0) {
			if (liveroom.getStartTime().compareTo(YMTDateUtil.getSysTime()) < 0) {
				Logger.verifyEquals(1, liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			} else {
				Logger.verifyEquals(liveroom.getLiveStatus(), liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			}
		}

		Logger.verifyEquals(liveroom.getLiveTitle(), liveRoomInfo.getLiveTitle(), "验证LiveTitle");
		Logger.verifyEquals(liveroom.getPlayUrl(), liveRoomInfo.getPlayUrl(), "验证PlayUrl");
		if (liveRoomInfo.getPreVideo() != null) {
			Logger.verifyEquals(liveroom.getPreVideoHeight(), liveRoomInfo.getPreVideo().getPreVideoHeight(), "验证getPreVideoHeight");
			Logger.verifyEquals(liveroom.getPreVideoLength(), liveRoomInfo.getPreVideo().getPreVideoLength(), "验证getPreVideoLength");
			Logger.verifyEquals(liveroom.getPreVideoUrl(), liveRoomInfo.getPreVideo().getPreVideoUrl(), "验证getPreVideoUrl");
			Logger.verifyEquals(liveroom.getPreVideoWidth(), liveRoomInfo.getPreVideo().getPreVideoWidth(), "验证getPreVideoWidth");
		}
		Logger.verifyEquals(liveroom.getUserId(), liveRoomInfo.getUserId(), "验证UserId");
		Logger.verifyEquals(liveroom.getLiveDesc(), liveRoomInfo.getLiveDesc(), "验证LiveDesc");
		Logger.verifyEquals(liveroom.getPutawayCount(), liveRoomInfo.getPutawayCount(), "验证PutawayCount");
		Map livestatusmap = liveRoomWapper.selectLiveRoomStatById(liveRoomInfo.getLiveId());
		if (livestatusmap != null) {
			Logger.verifyEquals(livestatusmap.get("ViewRealNum"), liveRoomInfo.getRealViewNum(), liveRoomInfo.getLiveId() + ":验证ViewRealNum");
			Logger.verifyEquals(livestatusmap.get("PreViewNum"), liveRoomInfo.getPreViewNum(), liveRoomInfo.getLiveId() + ":验证PreViewNum");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryByShopIndex_001() {
		Logger.start(true,"直播小铺首页列表成功");
		try {
			int size=5;
			Integer uid=liveRoomWapper.selectWxLives().get(0).getUserId();
			querybyshopindexBean.setPageIndex(1);
			querybyshopindexBean.setPageSize(size);
			List<Integer> sellerIdList=new ArrayList<>();
			sellerIdList.add(uid);
			querybyshopindexBean.setSellerIdList(sellerIdList);
			querybyshopindexCall.setData(querybyshopindexBean);
			querybyshopindexCall.callService();			
			Logger.verifyEquals("200", querybyshopindexCall.getString("code"), "验证code");
			Long count = liveRoomWapper.selectCountLiveroom(uid,1);
			Long pageCount = count / size + (count % size == 0 ? 0 : 1);
			Logger.verifyEquals(querybyshopindexCall.getTotalCount(), count.intValue(), "验证count");
			Logger.verifyEquals(querybyshopindexCall.getPageCount(), pageCount.intValue(), "验证pageCount");
			ArrayList<LiveRoomInfo> roomlist = querybyshopindexCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : roomlist) {
				verifyDB(liveRoomInfo);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QueryByShopIndex_002() {
		Logger.start(true,"不输入sellerIdList失败");
		try {
			int size=5;
			Integer uid=3383;
			querybyshopindexBean.setPageIndex(1);
			querybyshopindexBean.setPageSize(size);
			List<Integer> sellerIdList=new ArrayList<>();
			sellerIdList.add(uid);
//			querybyshopindexBean.setSellerIdList(sellerIdList);
			querybyshopindexCall.setData(querybyshopindexBean);
			querybyshopindexCall.callService();			
			Logger.verifyEquals("100", querybyshopindexCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryByShopIndex_003() {
		Logger.start(true,"输入多个sellerIdList成功");
		try {
			int size=5;
			Integer uid=liveRoomWapper.selectWxLives().get(0).getUserId();
			Integer uid2=liveRoomWapper.selectWxLives().get(1).getUserId();
			querybyshopindexBean.setPageIndex(1);
			querybyshopindexBean.setPageSize(size);
			List<Integer> sellerIdList=new ArrayList<>();
			sellerIdList.add(uid);
			sellerIdList.add(uid2);
			querybyshopindexBean.setSellerIdList(sellerIdList);
			querybyshopindexCall.setData(querybyshopindexBean);
			querybyshopindexCall.callService();			
			Logger.verifyEquals("200", querybyshopindexCall.getString("code"), "验证code");
			ArrayList<LiveRoomInfo> roomlist = querybyshopindexCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : roomlist) {
				verifyDB(liveRoomInfo);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QueryByShopIndex_004() {
		Logger.start(true,"不输入pageIndex失败");
		try {
			int size=5;
			Integer uid=3383;
//			querybyshopindexBean.setPageIndex(1);
			querybyshopindexBean.setPageSize(size);
			List<Integer> sellerIdList=new ArrayList<>();
			sellerIdList.add(uid);
			querybyshopindexBean.setSellerIdList(sellerIdList);
			querybyshopindexCall.setData(querybyshopindexBean);
			querybyshopindexCall.callService();			
			Logger.verifyEquals("100", querybyshopindexCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QueryByShopIndex_005() {
		Logger.start(true,"不输入pageSize失败");
		try {
			int size=5;
			Integer uid=3383;
			querybyshopindexBean.setPageIndex(1);
//			querybyshopindexBean.setPageSize(size);
			List<Integer> sellerIdList=new ArrayList<>();
			sellerIdList.add(uid);
			querybyshopindexBean.setSellerIdList(sellerIdList);
			querybyshopindexCall.setData(querybyshopindexBean);
			querybyshopindexCall.callService();			
			Logger.verifyEquals("100", querybyshopindexCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryByShopIndex_006() {
		Logger.start(true,"翻页查询成功");
		try {
			int size=5;
			Integer uid=liveRoomWapper.selectWxLives().get(0).getUserId();
			querybyshopindexBean.setPageIndex(2);
			querybyshopindexBean.setPageSize(size);
			List<Integer> sellerIdList=new ArrayList<>();
			sellerIdList.add(uid);
			querybyshopindexBean.setSellerIdList(sellerIdList);
			querybyshopindexCall.setData(querybyshopindexBean);
			querybyshopindexCall.callService();			
			Logger.verifyEquals("200", querybyshopindexCall.getString("code"), "验证code");
			Long count = liveRoomWapper.selectCountLiveroom(uid,1);
			Long pageCount = count / size + (count % size == 0 ? 0 : 1);
			Logger.verifyEquals(querybyshopindexCall.getTotalCount(), count.intValue(), "验证count");
			Logger.verifyEquals(querybyshopindexCall.getPageCount(), pageCount.intValue(), "验证pageCount");
			ArrayList<LiveRoomInfo> roomlist = querybyshopindexCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : roomlist) {
				verifyDB(liveRoomInfo);
			}	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}